<template>
  <div id="manager-recruiter">
    <el-container style="height: 100vh; border: 1px">
      <!-- 头部信息（复用HeaderBar组件） -->
      <el-header>
        <HeaderBar></HeaderBar>
      </el-header>
      <el-container style="flex: 1; display: flex;">
        <!-- 左侧栏 -->
        <el-aside :width="sidebarWidth" style="transition: width 0.3s ease;">
          <Sidebar @sidebar-toggle="handleSidebarToggle"></Sidebar>
        </el-aside>
        <!-- 主体内容区 -->
        <el-main :style="{
          marginLeft: '0',
          transition: 'margin-left 0.3s ease',
          width: `calc(100% - ${sidebarWidth})`
        }">
          <!-- <h2 style="font-size: 20px;font-weight: bold;">企业管理</h2> -->
          <el-row class="search">
            <input class="relative" v-model="searchKeyword" type="text" placeholder="搜索企业...">
            <el-button class="button" @click="handleSearch" icon="el-icon-search">搜索</el-button>
          </el-row>

          <!-- 企业信息列表 -->
          <div class="table-scroll-container">
            <el-table :data="tableData" class="recruiter-table" style="width: 100%">
              <!-- 企业名称列 -->
              <el-table-column prop="name" label="企业信息" width="280">
                <template slot-scope="scope">
                  <img :src="scope.row.avatar || defaultAvatar" class="company-avatar">
                  <span>{{ scope.row.name }}</span>
                </template>
              </el-table-column>
              <!-- 行业列 -->
              <el-table-column prop="companyIndustry" label="行业" width="180">
                <template slot-scope="scope">
                  <span>{{ scope.row.companyIndustry || '-' }}</span>
                </template>
              </el-table-column>
              <!-- 创建时间列 -->
              <el-table-column prop="contact" label="注册时间" width="180">
                <template slot-scope="scope">
                  <span>{{ formatDate(scope.row.createDate) || '-' }}</span>
                </template>
              </el-table-column>
              <!-- 工作时间 -->
              <el-table-column prop="workTimeType" label="工作时间" width="180">
                <template slot-scope="scope">
                  <span>{{ getWorkTimeTypeClass(scope.row.workTimeType) || '-' }}</span>
                </template>
              </el-table-column>
              <!-- 详细信息按钮列 -->
              <el-table-column label="详细信息">
                <template slot-scope="scope">
                  <el-button size="mini" type="primary" icon="el-icon-info" plain
                    @click="goToCompanyDetail(scope.row)"></el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页控件 -->
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="currentPage" :page-sizes="[5, 10, 20]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"  :total="totalCount">
          </el-pagination>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>

import Sidebar from "@/components/ManagerSidebar.vue";
import HeaderBar from "@/components/ManagerHeaderBar.vue";
import { getCompanyList } from "@/util/manager_api/company";

export default {
  components: {
    Sidebar,
    HeaderBar,
  },
  data() {
    return {
      searchKeyword: '',  // 搜索关键字
      tableData: [],  // 企业数据
      defaultAvatar: 'https://picsum.photos/200/200?random=0',
      selectedCompany: null,       // 选中的企业数据
      companyDetail: "",          // 企业详情内容
      currentPage: 1,
      pageSize: 5,
      totalCount: 0,
      isLoading: false, // 加载状态
      isSidebarCollapsed: false,
    };
  },
  computed: {
    sidebarWidth() {
      return this.isSidebarCollapsed ? '30px' : '200px';
    }
  },
  created() {
    this.fetchCompanyList();  // 初始化获取企业列表
  },
  watch: {
    // 监听搜索关键词变化
    searchKeyword(newVal, oldVal) {
      if (oldVal && !newVal.trim()) {
        // console.log('搜索关键词清空，重新获取完整数据');
        this.resetAndFetch();
      }
    }
  },
  methods: {
    resetAndFetch() {
      this.currentPage = 1;
      this.fetchCompanyList();
    },
    // 获取企业列表（包含搜索条件）
    async fetchCompanyList() {
      try {
        this.isLoading = true; // 显示加载状态
        const params = {
          page: this.currentPage,
          size: this.pageSize,
          content: this.searchKeyword || '', // 传递搜索关键词
        };
        const response = await getCompanyList(params);

        console.log('获取的公司列表为', response.data.data);
        this.tableData = response.data.data.respList || [];
        this.totalCount = response.data.data.total || 0;

        return this.tableData;
      } catch (error) {
        console.error('获取公司列表失败:', error);
        // 这里可以添加错误提示
        throw error;
      } finally {
        this.isLoading = false; // 隐藏加载状态
      }
    },

    //查看，企业详情前往企业页面
    goToCompanyDetail(company) {
      localStorage.setItem('companyId', company.companyId);
      this.$router.push({ name: 'company-detail' });
    },

    // 处理搜索
    handleSearch() {
      this.currentPage = 1; // 搜索时重置到第一页
      this.fetchCompanyList(); // 重新获取数据
    },

    // 处理分页大小变更
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchCompanyList();
    },

    // 处理当前页变更
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchCompanyList();
    },

    // 获取企业工作时间类型文本
    getWorkTimeTypeClass(type) {
      const classMap = {
        'SINGLE_REST': '单休',
        'DOUBLE_REST': '双休',
        'FLEXIBLE': '弹性工作时间'
      };
      return classMap[type] || '未说明';
    },
    // 格式化日期
    formatDate(timestamp) {
      if (!timestamp) return '';
      const date = new Date(timestamp);
      return date.toLocaleDateString() + ' ' + date.toTimeString().substr(0, 5); // 显示到秒
    },
    handleSidebarToggle(isCollapsed) {
      this.isSidebarCollapsed = isCollapsed;
    },
  }
};
</script>

<style scoped>

/* 侧边栏 */
.el-aside {
  background-color: white;
  color: #333;
  text-align: center;
  line-height: 200px;
  width: 200px;
}

/* 主体内容 */
.el-main {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  background-color: #f8f9fa;
  color: #333;
  font-size: 20px;
  padding: 20px;
}

/* 搜索区域 */
.search {
  background-color: white;
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-radius: 4px;
  margin-bottom: 15px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.search>* {
  margin-right: 10px;
}

.relative {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  height: 32px;
  flex: 1;
  padding: 0 12px;
  font-size: 14px;
}

.relative:focus {
  outline: none;
  border-color: #409eff;
}

.button {
  border-radius: 4px;
  background-color: #409eff;
  border: none;
  color: white;
  height: 32px;
  padding: 0 16px;
  cursor: pointer;
  font-size: 14px;
}

.button:hover {
  background-color: #66b1ff;
  transform: translateY(-1px);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

/* 表格内容 */
.table-scroll-container {
  height: 370px;
  overflow-y: auto;
  margin-bottom: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  background-color: white;
  padding: 0 20px;
}

.table-scroll-container::-webkit-scrollbar {
  width: 6px;
}

.table-scroll-container::-webkit-scrollbar-thumb {
  background: #dcdfe6;
  border-radius: 3px;
}

/* 企业表格样式 */
.recruiter-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.recruiter-table thead th {
  background-color: #f0f2f5;
  font-weight: 500;
  padding: 16px 15px;
  border-bottom: 2px solid #ebeef5;
  position: sticky;
  top: 0;
  z-index: 10;
  color: #606266;
  font-size: 14px;
  text-align: left;
}

.recruiter-table tbody tr {
  border-bottom: 1px solid #f5f7fa;
  transition: all 0.2s;
}

.recruiter-table tbody tr:hover {
  background-color: #fafbfe;
}

/* 企业头像样式 */
.company-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
  vertical-align: middle;
  object-fit: cover;
  border: 1px solid #ebeef5;
}

/* 按钮样式 */
.el-button {
  border-radius: 4px;
  padding: 0 12px;
  font-size: 12px;
  transition: all 0.3s ease;
}

.el-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.el-pagination {
  display: flex;
  justify-content: center;
  padding: 10px 0;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
</style>